package com.jims.api.v1.controller.patient;

import com.jims.master.common.data.Message;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.SecurityUtil;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.patient.health.entity.DailyCheck;
import com.jims.master.modules.patient.health.service.DailyCheckService;
import com.jims.master.modules.patient.myFamily.entity.MyFamily;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/27.
 */
@RestController
@RequestMapping(value = "dailyCheck")
public class DailyCheckController {
    @Autowired
    private DailyCheckService dailyCheckService;

    /**
     * @param userId 用户id
     * @param checkRes 检查结果 高压、身高
     * @param lowPress 低压
     * @param checkDate 检查日期
     * @param type 检查项目
     * @Description: （保存日常检查项目）
     * @author jict
     * @date 2017/11/27
     */
    @RequestMapping(value = "V100/saveDailyCheck", method = {RequestMethod.GET, RequestMethod.POST})
    public String saveDailyCheck(@RequestParam(required = true) String userId, @RequestParam(required = true) String item, @RequestParam(required = true) String checkRes, String lowPress, @RequestParam(required = true) String checkDate, String type,String expand1,String expand2,String expand3,String expand4
    ,String weight,String waistline) {
        Message msa = new Message();
        DailyCheck dailyCheck = new DailyCheck();
        if (StringUtils.isNotBlank(expand1)){
            dailyCheck.setExpand1(expand1);
        }
        if (StringUtils.isNotBlank(expand2)){
            dailyCheck.setExpand2(expand2);
        }
        if (StringUtils.isNotBlank(expand3)){
            dailyCheck.setExpand3(expand3);
        }
        if (StringUtils.isNotBlank(expand4)){
            dailyCheck.setExpand4(expand4);
        }
        if (StringUtils.isNotBlank(weight)){
            dailyCheck.setWeight(weight);
        }
        if (StringUtils.isNotBlank(waistline)){
            dailyCheck.setWaistline(waistline);
        }
        dailyCheck.setBaseInfo(userId);
        dailyCheck.setCheckRes(checkRes);
        dailyCheck.setLowPress(lowPress);
        dailyCheck.setItem(item);
        if (type != null) {
            dailyCheck.setType(type);
        }
        dailyCheck.setCheckDate(checkDate);
        int result = dailyCheckService.save(dailyCheck);
        if (result == 1) {
            msa.setData("保存成功");
        } else {
            msa.setData("保存失败");
        }
        return SecurityUtil.encryptMsgData(msa);
    }

    /**
     * @param userId 用户id
     * @param item 检查项目
     * @Description: （根据日常检查项目和userid查找对应记录）
     * @author jict
     * @date 2017/11/27
     */
    @RequestMapping(value = "V100/findDailyCheckList", method = {RequestMethod.GET, RequestMethod.POST})
    public String findDailyCheckList(@RequestParam(required = true) String userId, @RequestParam(required = true) String item, @RequestParam(required = true)String pageNo, String pageSize, HttpServletRequest request, HttpServletResponse response) {
        Page<DailyCheck> page = new Page<DailyCheck>();
        Map<String ,String> map = new HashMap<String, String>();
        map.put("userId",userId);
        map.put("item",item);
        map.put("pageNo",pageNo);
        map.put("pageSize",pageSize);
        page = dailyCheckService.findDailyCheckList(new Page<DailyCheck>(request, response), map);
        Message msa = new Message();
        List<DailyCheck> list = page.getList();
        msa.setData(list);
        return SecurityUtil.encryptMsgData(msa);
    }

}